//
//  IcAntiIcingTipsViewController.m
//  IceClear
//
//  Created by P & C on 2/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "IcAntiIcingTipsViewController.h"


@implementation IcAntiIcingTipsViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
		NSArray* materialsSection = [NSArray arrayWithObjects:@"Materials", [NSArray arrayWithObject:@"Fluids are the most commonly used materials for anti-icing.\nLiquid salt is the most effective for temperatures above -9.4 C. Below that, the most common fluid is a mixture of water and salt called brine which is effective to -21 C. It is possible to get equipment to make your own brine.\nOther fluids include calcium chloride, magnesium chloride, potassium acetate, potassium chloride and calcium magnesium acetate."], nil];
		NSArray* productsSection = [NSArray arrayWithObjects:@"Recommended Products", 
										[NSArray arrayWithObjects:[NSArray arrayWithObjects:@"Geomelt - (click to visit)", @"image",
																						[NSURL URLWithString:@"http://www.geomelt.ca/"], nil], 
															  [NSArray arrayWithObjects:@"AcuBrine - (click to visit)", @"image",
																						[NSURL URLWithString:@"http://www.cargill.com/products/salt/winter/accubrine/index.jsp"], nil], 
															   nil], 
									nil];
		tblTipsData = [NSMutableArray array];
		
		[tblTipsData addObject:materialsSection];
		[tblTipsData addObject:productsSection];
		[tblTipsData retain];
    }
    return self;
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


#pragma mark UITableView delegation
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	//if (tableView.tag == 0) {
		return [self numberOfSectionsInTableViewForTblTips: tableView];
	//}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	//if (tableView.tag == 0) {
		return [self numberOfRowsInSectionForTblTips: section];
	//}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	//if (tableView.tag == 0) {
		return [self cellForRowAtIndexPathForTblTips: indexPath];
	//}
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	//if (tableView.tag == 0) {
		return [self titleForHeaderInSectionForTblTips: section];
	//}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	//if (tableView.tag == 0) {
		return [self heightForRowAtIndexPathForTblTips: indexPath];
	//}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	//if (tableView.tag == 0) {
		return [self didSelectRowAtIndexPathForTblTips: indexPath];
	//}
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
	//if (tableView.tag == 0) {
		return [self viewForFooterInSectionForTblTips: section];
	//}
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
	//if (tableView.tag == 0) {
		return [self heightForFooterInSectionForTblTips: section];
	//}
}


- (NSInteger)numberOfSectionsInTableViewForTblTips:(UITableView *)tableView {
	return [tblTipsData count];
}

- (NSInteger)numberOfRowsInSectionForTblTips:(NSInteger)section {
	return [[[tblTipsData objectAtIndex:section] objectAtIndex:1] count];
}

- (CGFloat)heightForRowAtIndexPathForTblTips:(NSIndexPath *)indexPath {
	float val = 175;
	
	switch (indexPath.section) {
		case 1:
			val = 30;
			break;
		default:
			break;
	}
	
	return val;
}

- (void)didSelectRowAtIndexPathForTblTips:(NSIndexPath *)indexPath {
	if (indexPath.section == 1) {
		[[UIApplication sharedApplication] openURL:[[[[tblTipsData objectAtIndex:1] objectAtIndex:1] objectAtIndex:indexPath.row] objectAtIndex: 2]];
	}
}

- (UITableViewCell *)cellForRowAtIndexPathForTblTips:(NSIndexPath *)indexPath {
	static NSString* tblCellIdentifier = @"identifier.tbl.reuseablecell";
    UITableViewCell* cell = [tblTips dequeueReusableCellWithIdentifier:tblCellIdentifier];
	
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tblCellIdentifier] autorelease];
    }
	
	if (indexPath.section == 1) {
		cell.textLabel.text = [[[[tblTipsData objectAtIndex:1] objectAtIndex:1] objectAtIndex:indexPath.row] objectAtIndex: 0];
		//cell.imageView.image = [UIImage imageNamed:[[[tblTipsData objectAtIndex:1] objectAtIndex:(indexPath.row+1)] objectAtIndex:2]];
	}
    else {
		cell.textLabel.numberOfLines = 0;
		cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
		cell.textLabel.font = [UIFont systemFontOfSize:12];
		cell.textLabel.text = [[[tblTipsData objectAtIndex:indexPath.section] objectAtIndex:1] objectAtIndex:indexPath.row];
	}
	
    return cell;
}

- (UIView *)viewForFooterInSectionForTblTips:(NSInteger)section {
	if (section == 0) {
		UILabel* l = [[UILabel alloc] init];
		l.text = @"[Link Here to Screen #9]";
		[l setFrame:CGRectZero];
		[l setTextAlignment:UITextAlignmentCenter];
		[l setBackgroundColor:[UIColor clearColor]];
		
		return l;
	}
	
	return nil;
}

- (CGFloat)heightForFooterInSectionForTblTips:(NSInteger)section {
	return 20;
}

- (NSString *)titleForHeaderInSectionForTblTips:(NSInteger)section {
	return [[tblTipsData objectAtIndex:section] objectAtIndex: 0];
}


- (void)dealloc {
	[tblTips release];
	[tblTipsData release];
    [super dealloc];
}


@end
